<template>
  <div class="select-properties">
    <el-form label-position="top">
      <el-form-item label="占位文本">
        <el-input
          v-model="properties.placeholder"
          placeholder="请输入占位文本"
          @change="updateProperties" />
      </el-form-item>

      <el-form-item label="选项列表">
        <div v-for="(option, index) in properties.options" :key="index" class="option-item">
          <el-row :gutter="10">
            <el-col :span="10">
              <el-input
                v-model="option.label"
                placeholder="选项文本"
                @change="updateProperties" />
            </el-col>
            <el-col :span="10">
              <el-input
                v-model="option.value"
                placeholder="选项值"
                @change="updateProperties" />
            </el-col>
            <el-col :span="4">
              <el-button type="danger" @click="removeOption(index)">
                <el-icon><Delete /></el-icon>
              </el-button>
            </el-col>
          </el-row>
        </div>
        
        <el-button type="primary" @click="addOption">
          <el-icon><Plus /></el-icon>
          添加选项
        </el-button>
      </el-form-item>

      <el-form-item>
        <el-checkbox
          v-model="properties.clearable"
          @change="updateProperties">
          显示清除按钮
        </el-checkbox>
      </el-form-item>

      <el-form-item>
        <el-checkbox
          v-model="properties.multiple"
          @change="updateProperties">
          多选模式
        </el-checkbox>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { ref, watch } from 'vue'
import { Delete, Plus } from '@element-plus/icons-vue'

const props = defineProps({
  value: {
    type: Object,
    default: () => ({
      placeholder: '请选择',
      options: [],
      clearable: true,
      multiple: false
    })
  }
})

const emit = defineEmits(['update'])

const properties = ref({ ...props.value })

watch(() => props.value, (newVal) => {
  properties.value = { ...newVal }
}, { deep: true })

const addOption = () => {
  properties.value.options.push({
    label: '',
    value: ''
  })
  updateProperties()
}

const removeOption = (index) => {
  properties.value.options.splice(index, 1)
  updateProperties()
}

const updateProperties = () => {
  emit('update', { ...properties.value })
}
</script>

<style scoped>
.option-item {
  margin-bottom: 10px;
}
</style> 